<template>
  <div class="account-settings-info-view">
    <a-row :gutter="16" type="flex" justify="center">
      <a-col :order="isMobile ? 2 : 1" :md="24" :lg="16">
        <a-form layout="vertical">
          <a-form-item :label="$t('account.settings.basic.nickname')">
            <a-input v-model="queryParam.nickname" :placeholder="$t('account.settings.basic.nickname-message')" />
          </a-form-item>
          <a-form-item label="性别">
            <a-select v-model="queryParam.sex" placeholder="请选择">
              <a-select-option value="0">男</a-select-option>
              <a-select-option value="1">女</a-select-option>
              <a-select-option value="2">未知</a-select-option>
            </a-select>
          </a-form-item>
          <a-form-item label="密码">
            <a-input-password v-model="queryParam.password" placeholder="密码" autocomplete="current-password" />
          </a-form-item>
          <a-form-item label="gitee绑定">
            <a-input v-model="queryParam.giteeLogin" placeholder="请输入您的gitee账号" />
          </a-form-item>
          <a-form-item label="github绑定">
            <a-input v-model="queryParam.githubLogin" placeholder="请输入您的github账号" />
          </a-form-item>
          <a-form-item label="电话">
            <a-input v-model="queryParam.phone" placeholder="请输入您的手机号" />
          </a-form-item>

          <!-- <a-form-item :label="$t('account.settings.basic.profile')">
            <a-textarea rows="4" :placeholder="$t('account.settings.basic.profile-message')" />
          </a-form-item> -->

          <a-form-item :label="$t('account.settings.basic.email')" :required="false">
            <a-input v-model="queryParam.email" placeholder="example@ant.design" />
          </a-form-item>

          <a-form-item>
            <a-button type="primary" @click="submit">{{ $t('account.settings.basic.update') }}</a-button>
          </a-form-item>
        </a-form>
      </a-col>
      <a-col :order="1" :md="24" :lg="8" :style="{ minHeight: '180px' }">
        <div class="ant-upload-preview" @click="$refs.modal.edit(1)">
          <a-icon type="cloud-upload-o" class="upload-icon" />
          <div class="mask">
            <a-icon type="plus" />
          </div>
          <img :src="option.img" />
        </div>
      </a-col>
    </a-row>

    <avatar-modal ref="modal" @ok="setavatar" />
  </div>
</template>

<script>
import AvatarModal from './AvatarModal'
import { baseMixin } from '@/store/app-mixin'
import { mapGetters, mapActions, mapMutations } from 'vuex'
import { decryptStr } from '@/utils/passwordUtils'

export default {
  mixins: [baseMixin],
  components: {
    AvatarModal
  },
  data() {
    return {
      // cropper
      queryParam: {
        id: '',
        sex: '2',
        nickname: '',
        password: '',
        giteeLogin: '',
        githubLogin: '',
        phone: '',
        email: ''
      },
      preview: {},
      option: {
        img: '/avatar2.jpg',
        info: true,
        size: 1,
        outputType: 'jpeg',
        canScale: false,
        autoCrop: true,
        // 只有自动截图开启 宽度高度才生效
        autoCropWidth: 180,
        autoCropHeight: 180,
        fixedBox: true,
        // 开启宽度和高度比例
        fixed: true,
        fixedNumber: [1, 1]
      }
    }
  },
  computed: {
    ...mapGetters(['avatar'])
  },
  mounted() {
    // console.log('avatar', JSON.parse(this.avatar).url)
    this.getuserifno()

    this.option.img = JSON.parse(this.avatar).url // 初始化时设置头像
  },
  watch: {
    avatar(newAvatar, old) {
      // console.log('avatar', old)
      // console.log('newavatar', newAvatar)

      this.option.img = JSON.parse(newAvatar).url // 当 avatar 更新时，更新 option.img
    }
  },
  methods: {
    ...mapActions(['GetInfo']),
    ...mapMutations(['SET_AVATAR']),
    submit() {
      console.log('提交', this.queryParam)
      this.$http.put('/sysUser/update', this.queryParam).then((res) => {
        if (res.code === 200) {
          this.$message.success('修改成功')

          this.getuserifno()
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    getuserifno() {
      // console.log('用户信息')
      // const { GetInfo } = this
      this.$http.get('/sysUser/infoAndRole').then((res) => {
        console.log('res', res.result)
        this.SET_AVATAR(res.result.avatar)
        if (res.code === 200) {
          this.queryParam = {
            ...res.result,
            id: res.result.id,
            nickname: res.result.nickname,
            sex: res.result.sex.toString(),
            password: decryptStr(res.result.slat, res.result.password),
            giteeLogin: res.result.giteeLogin,
            githubLogin: res.result.githubLogin,
            phone: res.result.phone,
            email: res.result.email
          }
        }
      })
    },
    setavatar(url) {
      this.getuserifno()

      this.option.img = url
    }
  }
}
</script>

<style lang="less" scoped>
.avatar-upload-wrapper {
  height: 200px;
  width: 100%;
}

.ant-upload-preview {
  position: relative;
  margin: 0 auto;
  width: 100%;
  max-width: 180px;
  border-radius: 50%;
  box-shadow: 0 0 4px #ccc;

  .upload-icon {
    position: absolute;
    top: 0;
    right: 10px;
    font-size: 1.4rem;
    padding: 0.5rem;
    background: rgba(222, 221, 221, 0.7);
    border-radius: 50%;
    border: 1px solid rgba(0, 0, 0, 0.2);
  }
  .mask {
    opacity: 0;
    position: absolute;
    background: rgba(0, 0, 0, 0.4);
    cursor: pointer;
    transition: opacity 0.4s;

    &:hover {
      opacity: 1;
    }

    i {
      font-size: 2rem;
      position: absolute;
      top: 50%;
      left: 50%;
      margin-left: -1rem;
      margin-top: -1rem;
      color: #d6d6d6;
    }
  }

  img,
  .mask {
    width: 100%;
    max-width: 180px;
    height: 100%;
    border-radius: 50%;
    overflow: hidden;
  }
}
</style>
